# -*- coding: utf-8 -*-

import re
from i_util.tools import str_obj, unicode_obj
from bdp.i_crawler.i_downloader.ttypes import CrawlStatus

class CheckMiddleware(object):
    def __init__(self, conf):
        self.conf = conf
        self.logger = conf.get('log')

    def process_response(self, request, response):
        if response==None:
            return response
        if response.content==None:
            return response
        if len(response.content) < request.check_size:
            request.identify_status = CrawlStatus.CRAWL_SizetooSmall
        return response
        # if 'check_body' in request.info and request.info['check_body']:
        #     response._content = str_obj(response.content)
        #     if not re.search(request.info['check_body'], response.content):
        #         response.status_code = 911
        #         response.reason = 'check body fail (re: %s)' % request.info['check_body']
        #-----------------------------------------------------------------------------------------------
        # if hasattr(request,'check_body') or hasattr(request,'check_body_not'):
        #     response._content = str_obj(response.content)
        #     check_body = request.check_body
        #     check_body_not = request.check_body_not
        #     if not check_body_not and not re.search(check_body, response.content):
        #         response.status_code = 911
        #         response.reason = 'check_body fail (re: %s)' % check_body
        #     elif not check_body and re.search(check_body_not, response.content):
        #         response.status_code = 911
        #         response.reason = 'check_body_not fail (re: %s)' % check_body_not
        #     elif check_body_not and re.search(check_body_not, response.content) and \
        #          check_body and not re.search(check_body, response.content):
        #             response.status_code = 911
        #             response.reason = 'check_body fail (re: %s) and check_body_not fail (re: %s)' % (check_body, check_body_not)

